package com.shujia.flink.window

import org.apache.flink.streaming.api.scala._

object Demo2CountWindow {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    val wordsDS: DataStream[String] = linesDS.flatMap(_.split(","))

    val kvDS: DataStream[(String, Int)] = wordsDS.map((_, 1))

    /**
     * countWindow(10): 滚动的统计窗口，相同的key每到10条数据计算一次
     * countWindow(10,5): 滑动的统计窗口，每个5条数据计算一次，计算最近10条数据
     */
    kvDS
      .keyBy(_._1)
      .countWindow(10,5)
      .sum(1)
      .print()

    env.execute()

  }

}
